package imagePr;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class TestImagesss {

	public static BufferedImage getImage(String imageName) {
		try {
			File input = new File(imageName);
			BufferedImage image = ImageIO.read(input);
			return image;
		} catch (IOException ie) {
			System.out.println("Error:" + ie.getMessage());
		}
		return null;
	}

	public static BufferedImage getNegativeImage(BufferedImage img) {
		int w1 = img.getWidth();
		int h1 = img.getHeight();
		// int value[][] = new int[w1][h1];
		BufferedImage gray = new BufferedImage(w1, h1, 1);
		int value, alpha, r, g, b;
		for (int i = 0; i < w1; i++) {
			for (int j = 0; j < h1; j++) {
				value = img.getRGB(i, j); // store value
				alpha = getAlpha(value);
				r = 255 - getRed(value);
				g = 255 - getGreen(value);
				b = 255 - getBlue(value);

				value = createRGB(alpha, r, g, b);
				gray.setRGB(i, j, value);
			}
		}
		return gray;
	}

	public static int createRGB(int alpha, int r, int g, int b) {
		int rgb = (alpha << 24) + (r << 16) + (g << 8) + b;
		return rgb;
	}

	public static int getAlpha(int rgb) {
		return (rgb >> 24) & 0xFF;
	}

	public static int getRed(int rgb) {
		return (rgb >> 16) & 0xFF;
	}

	public static int getGreen(int rgb) {
		return (rgb >> 8) & 0xFF;
	}

	public static int getBlue(int rgb) {
		return rgb & 0xFF;
	}

	public void getRGB(BufferedImage img) {
		int w1 = img.getWidth();
		int h1 = img.getHeight();
		int value[][] = new int[w1][h1];
		int valueR[][] = new int[w1][h1];
		int valueG[][] = new int[w1][h1];
		int valueB[][] = new int[w1][h1];
		int valueA[][] = new int[w1][h1];
		for (int i = 0; i < w1; i++) {
			for (int j = 0; j < h1; j++) {
				value[i][j] = img.getRGB(i, j); // store value
				valueR[i][j] = getRed(value[i][j]);
				valueG[i][j] = getGreen(value[i][j]);
				valueB[i][j] = getBlue(value[i][j]);
				valueA[i][j] = getAlpha(value[i][j]);
			}
		}
	}

	public static BufferedImage getImage(int[][] value) {
		int w1 = value.length;
		int h1 = value[0].length;
		BufferedImage newImg = new BufferedImage(w1, h1, 1);
		for (int i = 0; i < w1; i++)
			for (int j = 0; j < h1; j++)
				newImg.setRGB(i, j, value[i][j]);
		return newImg;
	}

	// public static boolean isInside(int width, int height, Rectangle clip) {
	// // check starting point at first
	// if (clip.x < width && clip.y < height) {
	// if ((clip.x + clip.width) <= width && (clip.y + clip.height) <= height) {
	// return true;
	// }
	// }
	// return false;
	// }
	// public static BufferedImage clipImage(BufferedImage img, Rectangle clip)
	// {
	// if (isInside(img.getWidth(), img.getHeight(), clip)) {
	// int value[][] = getRGB(img);
	// int newW = clip.width - clip.x;
	// int newH = clip.height - clip.y;
	// BufferedImage clippedImg = new BufferedImage(newW, newH, 1);
	// for (int i = 0; i < newW; i++) {
	// for (int j = 0; j < newH; j++) {
	// clippedImg.setRGB(i, j, value[clip.x + i][clip.y + j]);
	// }
	// }
	// return clippedImg;
	// }
	// return null;
	// }
}
